﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = string.Empty;

            string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
            string Password = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Password]);
            string Output = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Output]);

            #region
            if (!string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password))
            {
                ZhangZhao.BLL.H_User bllUser = new BLL.H_User();
                ZhangZhao.Model.H_User modelUser = bllUser.GetModel(UserName, Password);

                if (modelUser != null)
                {
                    ZhangZhao.Model.UserCurrentInfo UserCurrentInfo = new Model.UserCurrentInfo();
                    UserCurrentInfo.Birthday = modelUser.Birthday;
                    UserCurrentInfo.Sex = modelUser.Sex;
                    UserCurrentInfo.Signature = modelUser.Signature;

                    #region 获取用户头像和联系方式

                    ZhangZhao.BLL.H_UserAvatar bllAvatar = new BLL.H_UserAvatar();
                    ZhangZhao.Model.H_UserAvatar modelAvatar = bllAvatar.GetModelByCache(UserName);

                    if (modelAvatar != null)
                    {
                        UserCurrentInfo.Avatar = modelAvatar.Avatar;
                        UserCurrentInfo.AvatarLarge = modelAvatar.AvatarLarge;
                        UserCurrentInfo.AvatarThumb = modelAvatar.AvatarThumb;
                        UserCurrentInfo.CreatedTime = modelAvatar.CreatedTime;
                        UserCurrentInfo.ViewCount = modelAvatar.ViewCount;
                    }

                    ZhangZhao.BLL.H_UserContact bllContact = new BLL.H_UserContact();
                    ZhangZhao.Model.H_UserContact modelContact = bllContact.GetModelByCache(UserName);

                    if (modelContact != null)
                    {
                        UserCurrentInfo.City = modelContact.City;
                        UserCurrentInfo.District = modelContact.District;
                        UserCurrentInfo.Email = modelContact.Email;
                        UserCurrentInfo.HomeAddress = modelContact.HomeAddress;
                        UserCurrentInfo.Phone1 = modelContact.Phone1;
                        UserCurrentInfo.Phone2 = modelContact.Phone2;
                        UserCurrentInfo.Province = modelContact.Province;
                        UserCurrentInfo.QQ = modelContact.QQ;
                        UserCurrentInfo.Website = modelContact.Website;
                        UserCurrentInfo.WeiBo = modelContact.WeiBo;
                        UserCurrentInfo.WeiXin = modelContact.WeiXin;
                    }

                    ZhangZhao.BLL.H_UserRelationship bllRelation = new BLL.H_UserRelationship();
                    List<ZhangZhao.Model.H_UserRelationship> modelRelations = bllRelation.GetModelList(string.Format(" UserName = '{0}' ", UserName));

                    if (modelRelations != null && modelRelations.Count > 0)
                    {
                        foreach (ZhangZhao.Model.H_UserRelationship r in modelRelations)
                        {
                            if (r.RelationShip.Equals("0"))
                            {
                                UserCurrentInfo.FollowList += 1;
                            }
                            else if (r.RelationShip.Equals("1"))
                            {
                                UserCurrentInfo.FanList += 1;
                            }
                            else if (r.RelationShip.Equals("2"))
                            {
                                UserCurrentInfo.BlackList += 1;
                            }
                        }
                    }

                    ZhangZhao.BLL.H_UserGatherThirdPhoto bllGather = new BLL.H_UserGatherThirdPhoto();
                    List<ZhangZhao.Model.H_UserGatherThirdPhoto> listGather = bllGather.GetModelList(string.Format(" UserName = '{0}' ", UserName));

                    if (listGather != null && listGather.Count > 0)
                    {
                        UserCurrentInfo.GatherList = listGather.Count;
                    }

                    #endregion

                    if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        result = JsonConvert.SerializeObject(UserCurrentInfo);
                    }
                    else
                    {
                        using (MemoryStream Stream = new MemoryStream())
                        {
                            XmlSerializer xml = new XmlSerializer(typeof(ZhangZhao.Model.UserCurrentInfo));

                            try
                            {
                                xml.Serialize(Stream, UserCurrentInfo);
                            }
                            catch { }

                            Stream.Position = 0;
                            using (StreamReader sr = new StreamReader(Stream))
                            {
                                result = sr.ReadToEnd();
                            }

                            xml = null;
                        }
                    }
                }
            }
            #endregion

            Response.Clear();
            Response.Write(result);
            Response.End();
        }
    }
}